package com.liuzu.esys.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liuzu.esys.bo.GoodBo;
import com.liuzu.esys.common.LayuiPageVo;
import com.liuzu.esys.mapper.GoodMapper;
import com.liuzu.esys.pojo.Good;
import com.liuzu.esys.service.GoodService;
import com.liuzu.esys.vo.TypeBarVo;
import com.liuzu.esys.vo.TypePieVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("goodService")
public class GoodServiceImpl implements GoodService {

    @Autowired
    private GoodMapper goodMapper;

    @Override
    public List<Good> GoodList() {
        return goodMapper.selectByExample(null);
    }

    @Override
    public LayuiPageVo<Good> goodPageData(Map<String, Object> params) {
        // mybatis 的 分页插件
        Integer page = Integer.parseInt(params.get("page") + "");
        Integer limit = Integer.parseInt(params.get("limit") + "");
        PageHelper.startPage(page, limit);

        List<Good> goods = goodMapper.selectByParams(params);

        PageInfo<Good> goodPageInfo = new PageInfo<>(goods);
        //把mybatis 的 分页数据 封装到  layui 的分页数据结构中

        LayuiPageVo<Good> goodLayuiPageVo = new LayuiPageVo<>();
        goodLayuiPageVo.setCode(0);
        goodLayuiPageVo.setMsg("分页数据列表");
        // xxxxxxxxxxxxxxxxxx
        goodLayuiPageVo.setCount(goodPageInfo.getTotal());
        goodLayuiPageVo.setData(goodPageInfo.getList());
        return goodLayuiPageVo;
    }

    @Override
    public int deleteGood(Integer id) {
        return goodMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int updateGood(Good good) {
        int i = goodMapper.updateByPrimaryKeySelective(good);
        return i;
    }

    @Override
    public int addGood(Good good) {
        int i = goodMapper.insertSelective(good);
        return i;
    }

    @Override
    public int deleteSelectGood(String ids) {
        String[] arrId = ids.split(",");
        return goodMapper.deleteSelectGood(arrId);
    }

    @Override
    public List<GoodBo> goodExcelData(Good good,Map<String, Object> params) {
        List<Good> goods = goodMapper.selectByExample(good);
        List<GoodBo> GoodBos = goods.stream()
                .map((go) -> {
                    GoodBo goodBo = new GoodBo();
                    goodBo.setId(go.getId());
                    goodBo.setName(go.getName());
                    goodBo.setBirthday(go.getBirthday());
                    goodBo.setAddress(go.getAddress());
                    return goodBo;
                }).collect(Collectors.toList());
        return GoodBos;
    }

    @Override
    public List<TypeBarVo> typeBarData() {
        List<TypeBarVo> list = goodMapper.typeBarData();
        return list;
    }
    @Override
    public List<TypePieVo> typePieData() {
        List<TypePieVo> list = goodMapper.typePieData();
        return list;
    }

    @Override
    public LayuiPageVo<Good> userPushPageData(Map<String, Object> params) {
        // mybatis 的 分页插件
        Integer page = Integer.parseInt(params.get("page") + "");
        Integer limit = Integer.parseInt(params.get("limit") + "");
        PageHelper.startPage(page, limit);

        List<Good> goods = goodMapper.userPushList(params);

        PageInfo<Good> goodPageInfo = new PageInfo<>(goods);
        //把mybatis 的 分页数据 封装到  layui 的分页数据结构中

        LayuiPageVo<Good> goodLayuiPageVo = new LayuiPageVo<>();
        goodLayuiPageVo.setCode(0);
        goodLayuiPageVo.setMsg("分页数据列表");
        // xxxxxxxxxxxxxxxxxx
        goodLayuiPageVo.setCount(goodPageInfo.getTotal());
        goodLayuiPageVo.setData(goodPageInfo.getList());
        return goodLayuiPageVo;
    }
}
